<?php
class InvitesController extends AppController {

	var $name = 'Invites';

        var $components = array('Acl');

	function index($id= null) {
            $this->set('event_id', $id);
		$this->Invite->recursive = 0;
	        $this->set('invites', $this->paginate($this->Invite,
                        array('Invite.event_id'=>$id)));
               // $this->set('invite', $this->Invite->read(null, $id));

        }

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid invite', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('invite', $this->Invite->read(null, $id));
	}

	function add($id = null) {

            if (!$id) {
                $this->Session->setFlash(__('Invalid invite', true));
                $this->redirect(array('action' => 'index'));
            }

            $this->set('event_id', $id);



            $event_id = $id;
            //Setting the event id and id up like this is important so that
            //the necessary values can be passed into the invite when we create it.

            $this->set('invite', $this->Invite->read(null, $id));
            //   $idd=$event_id;
            $use = $this->data['Invite']['email_addr'];
            $idd = $this->data ['Invite']['event_id'];

            $event = $this->Invite->Event->find('first',array('conditions'=> array('Event.id'=>$id)));

            $host = $this->Session->read('host');

            $alias = $id.'-'.$event['Event']['event_name'];
            
            //Only the host who created the event and co-hosts can send invites
            if($this->Acl->check($host['email_addr'],$alias, 'create')){

                if (!empty($this->data)) {

                    $this->Invite->create();
                    $many = $this->Invite->User->find('count', array('conditions' => array('email_addr' => $use)));
                    //"Many" finds out if there are any users who have emails that match with the
                    //email address we have been given through the add invite page's form.
                    $already = $this->Invite->find('count', array
                                ('conditions' => array(
                                    array('invite.event_id' => $idd),
                                    array('invite.email_addr' => $use))));
                    //"Already" exists to make sure that the person we're trying to invite to the event
                    //hasn't already been invited once, thus if it finds a value greater than zero
                    //it will not save the new invite and flash an error message.
                    if ($already <= 0) {


                        //  $this->Session->setFlash($many);
                        if ($many > 0) {
                            $used = $this->Invite->User->find('list', array('fields' => array('id')
                                        , 'conditions' => array('email_addr' => $use)));
                           
                            $user_d = implode(' ', $used);
                            $this->data['Invite']['user_id'] = $user_d;
                            //The purpose of the above code is that given someone's email address
                            //We search through our users array and if they have a User_ID we will
                            //add that user ID automatically to the invitation type item that
                            //this function creates.

                            if ($this->Invite->save($this->data)) {

                                $url = '/invites/index/' . $this->data['Invite']['event_id'];
                                $this->redirect($url);
                            } else {
                                $this->Session->setFlash(__('The invite could not be saved. Please, try again.', true));
                            }
                        } else {
                            $this->Session->setFlash(__('No user with that email address found.', true));
                        }
                    } else {
                        $this->Session->setFlash(__("This user is already invited to this event!", true));
                    }
                    $events = $this->Invite->Event->find('list');
                    $users = $this->Invite->User->find('list');
                    $this->set(compact('events', 'users'));
                }

           }else{
                 $this->Session->setFlash(__('You are not the owner of this event. You cannot send invites.', true));
                 $this->redirect(array('controller'=>'events','action' => 'index'));
            }


         
	}

	function edit($id = null) {
	if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid invite', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Invite->save($this->data)) {
				$this->Session->setFlash(__('The invite has been saved', true));
				$this->redirect(array('action' => 'index', $id));
			} else {
				$this->Session->setFlash(__('The invite could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Invite->read(null, $id);
		}
		$events = $this->Invite->Event->find('list');
		$users = $this->Invite->User->find('list');
		$this->set(compact('events', 'users'));
	}

	function delete($id =null, $event_id=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for invite', true));
			$this->redirect(array('action'=>'index'));
		}
                  $this->Session->setFlash($id);

                  $event_id = $this->Invite->find('list', array('fields'=>array('event_id')
                 ,'conditions'=>array('id'=>$id)));

                       $this->Session->setFlash($event_id);

                  $e= implode(' ',$event_id);
           
                    $url = '/invites/index/' .$e;
                   //Becuase we can not pass this particular function both the inviation id to delete
                    //and the event idea to return to isntead we settle for passing it the more important
                    //one, the invitation id since it is the invitation's primary key.
                   //After we've done that the function searches for the event id of all invitations
                    //which match our invitatiosn primary key.  Needless to say there will only
                    //Be one of them of course, and so we take that invitatiosn event idea, implode it
                    //to turn it into a value we can use and then we create a url to
                    //relocated the user to in the event of a sucessful deletion.

                 if ($this->Invite->delete($id)) {
                
		$this->Session->setFlash(__('Invite deleted', true));

                 
                             $this->redirect($url);
                      
		}
                else
                {
		$this->Session->setFlash(__('Invite was not deleted', true));
                }
                //$this->redirect(array('action' => 'index');
             //   $this->redirect($url);
             
       }
}

?>